package com.live.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.live.model.Admin;
import com.live.model.Contract;
import com.live.service.AdminService;
import com.live.service.ContractService;
import com.live.util.StringUtil;

@WebServlet("/contractEdit")
public class ContractEditController extends HttpServlet implements Servlet {
	private final Logger logger = Logger.getLogger(ContractEditController.class);
	private static final long serialVersionUID = 1L;
	private AdminService adminService;
	private ContractService  contractService;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(true);
			Admin admin = (Admin) session.getAttribute("CurrentUser");
			if (admin != null) {
				adminService = new AdminService();
				contractService = new ContractService();
				List<Admin> list = adminService.getAdminName();
				request.setAttribute("list", list);
				int id = Integer.parseInt(request.getParameter("id").trim());
				Contract contract = contractService.getContract(id);
				request.setAttribute("contract", contract);
				request.getRequestDispatcher("WEB-INF/view/contractEdit.jsp")
						.forward(request, response);
			}
			request.getRequestDispatcher("login.jsp")
					.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			request.getRequestDispatcher("login.jsp")
					.forward(request, response);
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("执行合同修改doPost方法");
		String firstPartyName = request.getParameter("firstPartyName").trim();
		String partyAddres = request.getParameter("partyAddres").trim();
		String contacts = request.getParameter("contacts").trim();
		String telephone = request.getParameter("telephone").trim();
		String contractNo = request.getParameter("contractNo").trim();
		int contractAmount = Integer.parseInt(request.getParameter("contractAmount").trim());
		String startTime = request.getParameter("startTime").trim();
		String endTime = request.getParameter("endTime").trim();
		int whetherPay = Integer.parseInt(request.getParameter("whetherPay").trim());
		int adminId = Integer.parseInt(request.getParameter("adminId").trim());
		int id = Integer.parseInt(request.getParameter("id").trim());
		String remarks = request.getParameter("remarks").trim();
		try {
			firstPartyName = new String(firstPartyName.getBytes("ISO-8859-1"),"UTF-8");
			partyAddres = new String(partyAddres.getBytes("ISO-8859-1"),"UTF-8");
			contacts = new String(contacts.getBytes("ISO-8859-1"),"UTF-8");
			remarks = new String(remarks.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		contractService =new ContractService();
		Contract  contract = new Contract();
		contract.setId(id);
		contract.setFirstPartyName(firstPartyName);
		contract.setPartyAddres(partyAddres);
		contract.setContacts(contacts);
		contract.setTelephone(telephone);
		contract.setContractNo(contractNo);
		contract.setContractAmount(contractAmount);
		contract.setUnit("元");
		contract.setStartTime(startTime);
		contract.setEndTime(endTime);
		contract.setWhetherPay(whetherPay);
		if (whetherPay==1) {
			contract.setPayTime(StringUtil.getStrDate());
		}
			contract.setAdminId(adminId);	
			contract.setRemarks(remarks);
		contractService.editContract(contract);
		response.sendRedirect(request.getContextPath() + "/contractList?firstPartyName=");// 跳转
		logger.debug("------editContract  editContract = " + contract.getTelephone()+"=="+contract.getContractNo());
	}

}
